﻿<?php
	$url = "http://www.cool-proxy.net/proxies/http_proxy_list/page:1/sort:score/direction:desc";
    $options = array(
        'http' => array(
            'header'  => "User-agent: Mozilla/5.0 (Windows NT 6.2; rv:23.0) Gecko/20100101 Firefox/23.0; ",
            'method'  => 'GET',
            'content' => '',
        ),
    );
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    $begin_record_tag = 'document.write(Base64.decode("';
    $ip_end_tag = '"))</script>';
    $output = "<h4> Danh sách proxy tốt nhất, cập nhật mỗi lần refresh </h4>";
    $output .= "<table width=100% border=1>";
    $output .= "<tr><th width=50%>IP Address</th><th width=20%> Port </th> <th width=30%> Country </th></tr>";
    $offset = 0;
    $current_pos = strpos($result,$begin_record_tag);
    while($current_pos !== false)
    {
        // Find IP
        $ip_begin_index = $current_pos + strlen($begin_record_tag);
        if($ip_begin_index === false)
            break;
        $offset = $ip_begin_index + 1;
        $ip_end_index =  strpos($result,$ip_end_tag,$offset);
        if($ip_end_index === false)
            break;
        $ip = base64_decode(substr($result,$ip_begin_index,$ip_end_index - $ip_begin_index));
        $offset = $ip_end_index + 1;
        
        // Find port
        $port_begin_index = strpos($result,'<td>',$offset) + strlen('<td>');
        $offset = $port_begin_index + 1;
        if($port_begin_index === false)
            break;
        $port_end_index = strpos($result,'</td>',$offset);
        if($port_end_index === false)
            break;
        $port = substr($result,$port_begin_index,$port_end_index - $port_begin_index);
        $offset = $port_end_index + 1;
        
        // Find country
        $tmp = strpos($result,'</td>',$offset);
        $offset = $tmp + 1;
        $country_begin_index = strpos($result,'<td>',$offset) + strlen('<td>');
        if($country_begin_index === false)
            break;
        $offset = $country_begin_index + 1;
        $country_end_index = strpos($result,'</td>',$offset);
        if($country_end_index === false)
            break;
        $country = substr($result,$country_begin_index,$country_end_index - $country_begin_index);
        $offset = $country_end_index + 1;
        
        // Print Info
        $output .= "<tr><td> ".$ip." </td><td> ".$port." </td> <td> ".$country." </td></tr>";
        
        // Get next record
        $current_pos = strpos($result,$begin_record_tag,$offset);
        if($current_pos === false)
            break;
    }
    $output .= "</table>";
    echo $output;
?>